package model

import (
	"fmt"
	"project2/g"
	"time"
)

type AppFun struct {

	// 系统功能信息表[查询]
	Id uint `json:"id" gorm:"primaryKey;column:id"`

	// 所属应用[关系=AppInfo]
	AppId uint `json:"appId" gorm:"column:app_id"`

	// 功能名称[查询]
	Name string `json:"name" gorm:"column:name"`

	// 功能类型[数据字典=FunType]
	FunType uint `json:"funType" gorm:"column:fun_type"`

	// 功能类型[数据字典=FunType]1内部功能2外部功能
	FunScope uint `json:"funScope" gorm:"column:fun_scope"`

	// 所属者类型[数据字典=OwnerType]0应用功能1管理员追加功能2用户追加功能
	OwnerType uint `json:"ownerType" gorm:"column:owner_type"`

	// 移动端应用状态
	MobileState uint `json:"mobileState" gorm:"column:mobile_state"`

	// 移动端功能地址
	MobileUrl string `json:"mobileUrl" gorm:"column:mobile_url"`

	// 功能地址[查询]
	FunUrl string `json:"funUrl" gorm:"column:fun_url"`

	// 所属上级类别[关系=SysClass]
	ParentId uint `json:"parentId" gorm:"column:parent_id"`

	// 所有分类路径
	ParentIdPath string `json:"parentIdPath" gorm:"column:parent_id_path"`

	// 功能深度[查询]
	Deep uint `json:"deep" gorm:"column:deep"`

	// 分类图片[查询]
	Img string `json:"img" gorm:"column:img"`

	// 排序号
	OrderNum uint `json:"orderNum" gorm:"column:order_num"`

	// 当前状态[查询]
	State uint `json:"state" gorm:"column:state"`

	// 有效状态[数据字典=del_state]
	DelState uint `json:"delState" gorm:"column:del_state"`

	// 添加人
	AddUser string `json:"addUser" gorm:"column:add_user"`

	// 添加人ID
	AddUserId uint `json:"addUserId" gorm:"column:add_user_id"`

	// 添加时间
	AddTime time.Time `json:"addTime" gorm:"column:add_time"`

	// 最近更新人
	EditUser string `json:"editUser" gorm:"column:edit_user"`

	// 最近更新人ID
	EditUserId uint `json:"editUserId" gorm:"column:edit_user_id"`

	// 最近更新时间
	LastEditTime time.Time `json:"lastEditTime" gorm:"column:last_edit_time"`

	// 服务器编号ID
	ServerNumber uint `json:"serverNumber" gorm:"column:server_number"`

	//
	DelTime   time.Time  `json:"delTime" gorm:"column:del_time"`
	CreatedAt time.Time  `json:"createdAt" gorm:"column:created_at"`
	UpdatedAt time.Time  `json:"updatedAt" gorm:"column:updated_at"`
	DeletedAt *time.Time `json:"deletedAt" gorm:"column:deleted_at"`
}

func (AppFun) TableName() string {
	return "app_fun"
}
func (self *AppFun) UpdateEnity(id int, updatelog *string) {
	var oldData AppFun
	g.Db.Model(&AppFun{}).Where("id = ?", id).First(&oldData)
	if oldData.Id <= 0 {
		//todo error
		return
	}
	updateTx := g.Db.Model(&AppFun{}).Where("id = ?", id)
	updateEnity := &AppFun{}
	if self.Id != oldData.Id {
		updateEnity.Id = self.Id
		(*updatelog) += fmt.Sprintf("系统功能信息表[查询]从 %s 修改为 %s", oldData.Id, self.Id)
	}
	if self.AppId != oldData.AppId {
		updateEnity.AppId = self.AppId
		(*updatelog) += fmt.Sprintf("所属应用[关系=AppInfo]从 %s 修改为 %s", oldData.AppId, self.AppId)
	}
	if self.Name != oldData.Name {
		updateEnity.Name = self.Name
		(*updatelog) += fmt.Sprintf("功能名称[查询]从 %s 修改为 %s", oldData.Name, self.Name)
	}
	if self.FunType != oldData.FunType {
		updateEnity.FunType = self.FunType
		(*updatelog) += fmt.Sprintf("功能类型[数据字典=FunType]从 %s 修改为 %s", oldData.FunType, self.FunType)
	}
	if self.FunScope != oldData.FunScope {
		updateEnity.FunScope = self.FunScope
		(*updatelog) += fmt.Sprintf("功能类型[数据字典=FunType]1内部功能2外部功能从 %s 修改为 %s", oldData.FunScope, self.FunScope)
	}
	if self.OwnerType != oldData.OwnerType {
		updateEnity.OwnerType = self.OwnerType
		(*updatelog) += fmt.Sprintf("所属者类型[数据字典=OwnerType]0应用功能1管理员追加功能2用户追加功能从 %s 修改为 %s", oldData.OwnerType, self.OwnerType)
	}
	if self.MobileState != oldData.MobileState {
		updateEnity.MobileState = self.MobileState
		(*updatelog) += fmt.Sprintf("移动端应用状态从 %s 修改为 %s", oldData.MobileState, self.MobileState)
	}
	if self.MobileUrl != oldData.MobileUrl {
		updateEnity.MobileUrl = self.MobileUrl
		(*updatelog) += fmt.Sprintf("移动端功能地址从 %s 修改为 %s", oldData.MobileUrl, self.MobileUrl)
	}
	if self.FunUrl != oldData.FunUrl {
		updateEnity.FunUrl = self.FunUrl
		(*updatelog) += fmt.Sprintf("功能地址[查询]从 %s 修改为 %s", oldData.FunUrl, self.FunUrl)
	}
	if self.ParentId != oldData.ParentId {
		updateEnity.ParentId = self.ParentId
		(*updatelog) += fmt.Sprintf("所属上级类别[关系=SysClass]从 %s 修改为 %s", oldData.ParentId, self.ParentId)
	}
	if self.ParentIdPath != oldData.ParentIdPath {
		updateEnity.ParentIdPath = self.ParentIdPath
		(*updatelog) += fmt.Sprintf("所有分类路径从 %s 修改为 %s", oldData.ParentIdPath, self.ParentIdPath)
	}
	if self.Deep != oldData.Deep {
		updateEnity.Deep = self.Deep
		(*updatelog) += fmt.Sprintf("功能深度[查询]从 %s 修改为 %s", oldData.Deep, self.Deep)
	}
	if self.Img != oldData.Img {
		updateEnity.Img = self.Img
		(*updatelog) += fmt.Sprintf("分类图片[查询]从 %s 修改为 %s", oldData.Img, self.Img)
	}
	if self.OrderNum != oldData.OrderNum {
		updateEnity.OrderNum = self.OrderNum
		(*updatelog) += fmt.Sprintf("排序号从 %s 修改为 %s", oldData.OrderNum, self.OrderNum)
	}
	if self.State != oldData.State {
		updateEnity.State = self.State
		(*updatelog) += fmt.Sprintf("当前状态[查询]从 %s 修改为 %s", oldData.State, self.State)
	}
	if self.DelState != oldData.DelState {
		updateEnity.DelState = self.DelState
		(*updatelog) += fmt.Sprintf("有效状态[数据字典=del_state]从 %s 修改为 %s", oldData.DelState, self.DelState)
	}
	if self.AddUser != oldData.AddUser {
		updateEnity.AddUser = self.AddUser
		(*updatelog) += fmt.Sprintf("添加人从 %s 修改为 %s", oldData.AddUser, self.AddUser)
	}
	if self.AddUserId != oldData.AddUserId {
		updateEnity.AddUserId = self.AddUserId
		(*updatelog) += fmt.Sprintf("添加人ID从 %s 修改为 %s", oldData.AddUserId, self.AddUserId)
	}
	if self.AddTime != oldData.AddTime {
		updateEnity.AddTime = self.AddTime
		(*updatelog) += fmt.Sprintf("添加时间从 %s 修改为 %s", oldData.AddTime, self.AddTime)
	}
	if self.EditUser != oldData.EditUser {
		updateEnity.EditUser = self.EditUser
		(*updatelog) += fmt.Sprintf("最近更新人从 %s 修改为 %s", oldData.EditUser, self.EditUser)
	}
	if self.EditUserId != oldData.EditUserId {
		updateEnity.EditUserId = self.EditUserId
		(*updatelog) += fmt.Sprintf("最近更新人ID从 %s 修改为 %s", oldData.EditUserId, self.EditUserId)
	}
	if self.LastEditTime != oldData.LastEditTime {
		updateEnity.LastEditTime = self.LastEditTime
		(*updatelog) += fmt.Sprintf("最近更新时间从 %s 修改为 %s", oldData.LastEditTime, self.LastEditTime)
	}
	if self.ServerNumber != oldData.ServerNumber {
		updateEnity.ServerNumber = self.ServerNumber
		(*updatelog) += fmt.Sprintf("服务器编号ID从 %s 修改为 %s", oldData.ServerNumber, self.ServerNumber)
	}
	if self.DelTime != oldData.DelTime {
		updateEnity.DelTime = self.DelTime
		(*updatelog) += fmt.Sprintf("从 %s 修改为 %s", oldData.DelTime, self.DelTime)
	}
	updateTx.Updates(updateEnity)
}
func (self *AppFun) BeforeCreate() (err error) {
	self.CreatedAt = time.Now()
	self.AddTime = time.Now()
	return

}
func (self *AppFun) BeforeUpdate() (err error) {
	self.LastEditTime = time.Now()
	return
}
